package com.cv.platform.service.carousel;

import cn.hutool.core.collection.CollUtil;
import com.cv.framework.common.utils.object.BeanUtils;
import com.cv.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cv.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.cv.infra.api.file.FileApi;
import com.cv.platform.controller.platform.carousel.vo.CvVideoCarouselRespVO;
import com.cv.platform.controller.system.carousel.vo.CvVideoCarouselSaveReqVO;
import com.cv.platform.dal.dataobject.carousel.CvVideoCarouselDO;
import com.cv.platform.dal.dataobject.video.CvVideoDO;
import com.cv.platform.dal.mapper.carousel.CvVideoCarouselMapper;
import com.cv.platform.enums.VideoStatusEnum;
import com.cv.platform.enums.YesOrNoEnum;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 视频轮播图业务层接口实现类
 *
 * @author Charles_XDXD
 */
@Service
public class CvVideoCarouselServiceImpl implements CvVideoCarouselService {

    @Resource
    private CvVideoCarouselMapper cvVideoCarouselMapper;

    @DubboReference
    private FileApi fileApi;

    @Override
    public List<CvVideoCarouselRespVO> getAllList() {
        // 因为轮播图数据不会特别多，因此直接查询所有数据
        MPJLambdaWrapper<CvVideoCarouselDO> wrapper = new MPJLambdaWrapperX<CvVideoCarouselDO>()
                .selectAll(CvVideoCarouselDO.class)
                .leftJoin(CvVideoDO.class, CvVideoDO::getId, CvVideoCarouselDO::getVideoId)
                .selectAs(CvVideoDO::getThumbnail, CvVideoCarouselRespVO::getThumbnail)
                .eq(CvVideoDO::getIsVisible, YesOrNoEnum.YES.getCode())
                .eq(CvVideoDO::getStatus, VideoStatusEnum.PASS.getStatus())
                .eq(CvVideoDO::getDeleted, YesOrNoEnum.NO.getCode())
                .orderByAsc(CvVideoCarouselDO::getSorted);

        List<CvVideoCarouselRespVO> cvVideoCarouselDOList = cvVideoCarouselMapper.selectJoinList(CvVideoCarouselRespVO.class, wrapper);

        if (CollUtil.isEmpty(cvVideoCarouselDOList)) {
            return Collections.emptyList();
        }

        for (CvVideoCarouselRespVO cvVideoCarouselRespVO : cvVideoCarouselDOList) {
            cvVideoCarouselRespVO.setThumbnailUrl(fileApi.getUrl(cvVideoCarouselRespVO.getThumbnail()));
        }

        return cvVideoCarouselDOList;
    }

    @Override
    public void insertOrUpdate(CvVideoCarouselSaveReqVO reqVO) {
        CvVideoCarouselDO carouselDO = BeanUtils.toBean(reqVO, CvVideoCarouselDO.class);
        cvVideoCarouselMapper.insertOrUpdate(carouselDO);
    }

    @Override
    public void deleteCarousel(Long id) {
        cvVideoCarouselMapper.deleteById(id);
    }

    @Override
    public void deleteCarouselByVideoId(Long videoId) {
        LambdaQueryWrapperX<CvVideoCarouselDO> wrapper = new LambdaQueryWrapperX<CvVideoCarouselDO>()
                .eq(CvVideoCarouselDO::getVideoId, videoId);
        cvVideoCarouselMapper.delete(wrapper);
    }

}
